(function($, doc, room, chat) {
	mui.init();
	var settings = app.getSettings();
	var state = app.getState();
	var uid = state.user_id;
	$.plusReady(function() {
		var chatName = plus.webview.currentWebview().chatName;
		var roomId = plus.webview.currentWebview().roomId;
		var roomdata = room.get(roomId);
		var list = roomdata.list;
		var uname = state.name;
		var info = roomdata.info;
		//console.log(JSON.stringify(info));
		$.preload({
			"id": 'room_qrcode.html',
			"url": 'room_qrcode.html',
			createNew: false
		});
		//console.log(JSON.stringify(roomdata));
		var ukey = 0;
		for(var i = 0; i < list.length; i++) {
			/*
			var headerimg = list[i]['avatar'];
			if(headerimg && headerimg.indexOf('http') === -1 && headerimg.indexOf('upload') !== -1) {
				list[i]['avatar'] = serverurl + headerimg;
			} else {
				list[i]['avatar'] = '../../static/images/1.png';
			}*/
			list[i]['avatar'] = app.avatar(list[i]['user_id'], '../../static/images/1.png');
			if(list[i]['user_id'] == uid) {
				uname = list[i]['nickname'];
				ukey = i;
			}
			if(!list[i]['nickname']) {
				list[i]['nickname'] = list[i]['user_id'];
			}
			//console.log(list[i]['avatar']);
		}
		document.querySelector("#list-area").innerHTML = template('list-templete', {
			"list": list
		});
		doc.getElementById('username').innerHTML = uname;

		doc.getElementById('groupname').innerHTML = info['name'];
		doc.getElementById('groupname').addEventListener('tap', function() {
			var btnArray = ['确定', '取消'];
			mui.prompt('为群起一个好听的名字', '', '', btnArray, function(e) {
				if(e.index == 0) {
					if(e.value && e.value.length > 0) {
						var postdata = {
							user_id: uid,
							room_id: roomId,
							roomname: e.value
						};
						$.post(apiurl + 'chat/setroomname', postdata, function(res) {
							console.log(JSON.stringify(res));
							if(res.status > 0) {
								doc.getElementById('groupname').innerHTML = e.value;
								roomdata.info.name = e.value;
								room.edit(roomdata);
								//chat.changeName(roomId, e.value);
								chat.setval(roomId, 'name', e.value);
							} else {
								$.alert(res.msg);
							}
						});
					}
				} else {
					//info.innerText = '你点了取消按钮';
				}
			}, 'div');
		});
		doc.getElementById('usernamearea').addEventListener('tap', function() {
			var btnArray = ['确定', '取消'];
			mui.prompt('为自己起一个好听的名字', '', '', btnArray, function(e) {
				if(e.index == 0) {
					if(e.value && e.value.length > 0) {
						var postdata = {
							user_id: uid,
							room_id: roomId,
							nickname: e.value
						};
						$.post(apiurl + 'chat/setroomnickname', postdata, function(res) {
							if(res.status > 0) {
								doc.getElementById('username').innerHTML = e.value;
								roomdata['list'][ukey]['nickname'] = e.value;
								room.edit(roomdata);
							} else {
								$.alert('修改失败');
							}
						});
					}

					//mui.alert(e.value);
					//info.innerText = '谢谢你的评语：' + e.value;
				}
			}, 'div');
		});
		doc.getElementById('delroom').addEventListener('tap', function() {
			$.post(apiurl + 'chat/delRoom', {
				user_id: uid,
				room_id: roomId
			}, function(res) {
				room.del(roomId);
				chat.delOne(roomId);
				$.alert('成功退出');
				location.href = '../../chat.html';
			});
		});
		doc.getElementById('mgrRoom').addEventListener('tap', function(e) {
			e.stopPropagation();
			var id = plus.webview.getWebviewById('edit_room.html');
			if(id) {
				plus.webview.close(id, "none");
			}
			mui.openWindow({
				url: 'edit_room.html',
				id: 'edit_room.html',

				show: {
					autoShow: true,
					aniShow: 'slide-in-right',
					duration: 100
				},
				waiting: {
					autoShow: true,
					title: 'waiting...'
				},
				extras: {
					//chatName: chatName,
					roomId: roomId
				}
			});
		});

		if(document.getElementById('topmsg')) {
			var istop = chat.getval(roomId, 'istop') || 0;
			if(istop > 0) {
				document.querySelector("#topmsg").classList.add("mui-active");
			} else {
				document.querySelector("#topmsg").classList.remove("mui-active");
			}
			document.getElementById('topmsg').addEventListener('tap', function() {
				var sval = document.querySelector("#topmsg").classList.contains("mui-active") ? 1 : 0;
				chat.setval(roomId, 'istop', sval);
				mui.toast('置顶成功！');
			});
		}

		if(document.getElementById('clearmsg')) {
			document.getElementById('clearmsg').addEventListener('tap', function() {

				chat.removeChat(roomId);
				mui.toast('清除成功！');
				mui.fire(plus.webview.currentWebview().opener(), 'clear');
			});
		}

		doc.getElementById('qrcode').addEventListener('tap', function() {
			//console.log(this);
			//console.log(JSON.stringify(info));
			var service_comfirm = plus.webview.getWebviewById("room_qrcode.html");
			service_comfirm && plus.webview.close(service_comfirm, "none");
			mui.openWindow({
				url: 'room_qrcode.html',
				id: 'room_qrcode.html',

				show: {
					autoShow: true,
					aniShow: 'slide-in-right',
					duration: 100
				},
				waiting: {
					autoShow: true,
					title: 'waiting...'
				},
				extras: {
					chatName: info.name,
					roomId: info.user_id
				}
			});
		});
	});


	mui("#list-area").on('tap', 'li', function() {
		var header = this.getAttribute("header");
		var chatName = this.getAttribute("chatName");
		var fid = this.getAttribute("fid");
		mui.openWindow({
			url: '../user/other_detail.html',
			id: '../user/other_detail.html',

			show: {
				autoShow: true,
				aniShow: 'slide-in-right',
				duration: 100
			},
			waiting: {
				autoShow: true,
				title: 'waiting...'
			},
			extras: {
				chatHeader: header,
				chatName: chatName,
				fid: fid
			}
		})
	});

}(mui, document, room, chat));